package com.ssyt.ec.que.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.ssyt.ec.common.AppServlet;
import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.DBUtil;
import com.ssyt.ec.common.util.FastjsonUtil;
import com.ssyt.ec.que.entity.QuestionEntity;
import com.ssyt.ec.que.service.QestionsService;

/**
 * 试题
 * 
 * @author Administrator
 *
 */
@WebServlet("/que/QestionsServlet")
public class QestionsServlet extends AppServlet {
	private static final long serialVersionUID = 1L;

	public QestionsServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("listQestionsOp".equals(command)) {
			listQestionsOp(request, response);
		} else if ("listQestionsClo".equals(command)) {
			listQestionsClo(request, response);
		} else if ("addQue".equals(command)) {
			addQue(request, response);
		} else if ("getQue".equals(command)) {
			getQue(request, response);
		} else if ("updateQue".equals(command)) {
			updateQue(request, response);
		} else if ("updateState".equals(command)) {
			updateState(request, response);
		}
	}

	/**
	 * 修改状态
	 * 
	 * @param request
	 * @param response
	 */
	private void updateState(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		String val = request.getParameter("value");
		int value = 0;
		if ("禁用".equals(val)) {
			value = 2;
		} else if ("启用".equals(val)) {
			value = 1;
		}
		QestionsService qestionsService = new QestionsService();
		int data = qestionsService.updateStateQue(id, value);
		print(response, FastjsonUtil.toJson(data));
	}

	/**
	 * 修改试题
	 * 
	 * @param request
	 * @param response
	 */
	private void updateQue(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		String type = request.getParameter("type");
		String difficulty = request.getParameter("difficulty");
		String question = request.getParameter("question");
		String a = request.getParameter("a");
		String b = request.getParameter("b");
		String c = request.getParameter("c");
		String d = request.getParameter("d");
		String answer = request.getParameter("answer");
		int type1 = 0;
		if ("1".equals(type) || "单选".equals(type)) {
			type1 = 1;
		} else if ("2".equals(type) || "多选".equals(type)) {
			type1 = 2;
		} else if ("3".equals(type) || "判断".equals(type)) {
			type1 = 3;
		}
		int difficulty1 = 0;
		if ("1".equals(difficulty) || "低".equals(difficulty)) {
			difficulty1 = 1;
		} else if ("2".equals(difficulty) || "中".equals(difficulty)) {
			difficulty1 = 2;
		} else if ("3".equals(difficulty) || "较高".equals(difficulty)) {
			difficulty1 = 3;
		} else if ("4".equals(difficulty) || "高".equals(difficulty)) {
			difficulty1 = 4;
		}
		QestionsService qestionsService = new QestionsService();
		int data = qestionsService.updateQue(id, question, a, b, c, d, answer,
				type1, difficulty1);
		print(response, FastjsonUtil.toJson(data));
	}

	/**
	 * 试题信息的回显
	 * 
	 * @param request
	 * @param response
	 */
	private void getQue(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		System.out.println(id);
		QuestionEntity role = DBUtil
				.getUniqueResult(
						QuestionEntity.class,
						"SELECT q.*,s.name FROM qes_qestions q,sys_course s WHERE q.course_id=s.id  AND q.id=?",
						id);
		if (role != null) {
			print(response, FastjsonUtil.toJson(role));

		}
	}

	/**
	 * 试题新增
	 * 
	 * @param request
	 * @param response
	 */
	private void addQue(HttpServletRequest request, HttpServletResponse response) {
		QestionsService qestionsService = new QestionsService();
		String course = request.getParameter("course");
		int type = Integer.parseInt(request.getParameter("type"));
		int difficulty = Integer.parseInt(request.getParameter("difficulty"));
		String question = request.getParameter("question");
		String a = request.getParameter("a");
		String b = request.getParameter("b");
		String c = request.getParameter("c");
		String d = request.getParameter("d");
		String answer = request.getParameter("answer");
		System.out.println(course + "," + type + "," + difficulty + ","
				+ question + "," + a + "," + b + "," + c + "," + d + ","
				+ answer);
		int data = qestionsService.addQes(course, type, difficulty, question,
				a, b, c, d, answer);
		print(response, FastjsonUtil.toJson(data));
	}

	/**
	 * 全部有效试题的查询
	 * 
	 * @param request
	 * @param response
	 */
	private void listQestionsOp(HttpServletRequest request,
			HttpServletResponse response) {
		QestionsService qestionsService = new QestionsService();
		String question = request.getParameter("SearchName");
		String cousreSearch = request.getParameter("cousreSearch");
		String typeSearch = request.getParameter("typeSearch");
		String difSearch = request.getParameter("difSearch");
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		if (StringUtils.isNoneBlank(question)) {
			PageModel<QuestionEntity> list = qestionsService.selectQes(
					question, page, rows);
			print(response,
					FastjsonUtil.toJsonWithDateFormat(list, "yyyy-MM-dd"));
		} else if (StringUtils.isNoneBlank(cousreSearch)
				&& !"1".equals(cousreSearch)) {
			int cousre = Integer.parseInt(cousreSearch);
			PageModel<QuestionEntity> list = qestionsService.selectCousre(
					cousre, page, rows);
			print(response,
					FastjsonUtil.toJsonWithDateFormat(list, "yyyy-MM-dd"));
		} else if (StringUtils.isNoneBlank(typeSearch)) {
			int type = Integer.parseInt(typeSearch);
			PageModel<QuestionEntity> list = qestionsService.selectType(type,
					page, rows);
			print(response,
					FastjsonUtil.toJsonWithDateFormat(list, "yyyy-MM-dd"));
		} else if (StringUtils.isNoneBlank(difSearch)) {
			int dif = Integer.parseInt(difSearch);
			PageModel<QuestionEntity> list = qestionsService.selectDif(dif,
					page, rows);
			print(response,
					FastjsonUtil.toJsonWithDateFormat(list, "yyyy-MM-dd"));
		} else {
			PageModel<QuestionEntity> list = qestionsService.listQuestioinsOp(
					page, rows);
			print(response,
					FastjsonUtil.toJsonWithDateFormat(list, "yyyy-MM-dd"));
		}

	}

	/**
	 * 全部无效试题的查询
	 * 
	 * @param request
	 * @param response
	 */
	private void listQestionsClo(HttpServletRequest request,
			HttpServletResponse response) {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		QestionsService qestionsService = new QestionsService();
		PageModel<QuestionEntity> list = qestionsService.listQuestioinsClo(
				page, rows);
		print(response, FastjsonUtil.toJsonWithDateFormat(list, "yyyy-MM-dd"));
	}
}
